﻿using System.Windows;
using System.Windows.Controls;
using nRoute.ViewServices;
using System.Collections.Generic;
using nRoute.Samples.Sudoku.ViewServices;

namespace nRoute.Samples.Sudoku.Controls
{
    [MapViewService(typeof(IWorkingIndicatorViewService), Lifetime = ViewServiceLifetime.DiscoveredInstance)]
    public partial class WorkingIndicator : UserControl, IWorkingIndicatorViewService
    {
        private readonly List<object> _tokens;

        public WorkingIndicator()
        {
            InitializeComponent();

            _tokens = new List<object>();
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
                LayoutRoot.Visibility = Visibility.Collapsed;
            
        }  

#region IWorkingIndicator Members

        public void Show(object token)
        {
            _tokens.Add(token);
            if (LayoutRoot.Visibility != Visibility.Visible)
            {
                LayoutRoot.Visibility = Visibility.Visible;
                IndicatorStoryboard.Begin();
            }
        }

        public void Hide(object token)
        {
            _tokens.Remove(token);      // notee we don't check if it exists
            if (_tokens.Count == 0)
            {
                LayoutRoot.Visibility = Visibility.Collapsed;
                IndicatorStoryboard.Stop();
            }
        }

#endregion

    }
}
